package com.sh.weblogspringboot.admin.subscriber;

import com.sh.weblogspringboot.admin.event.ReadArticleEvent;
import com.sh.weblogspringboot.common.domain.mapper.ArticleMapper;
import com.sh.weblogspringboot.common.domain.mapper.StatisticsArticlePVMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author songHai
 * @Classname ReadArticleSubscriber
 * @Description 阅读文章事件订阅者
 * @Date 2025/4/13 11:54
 */
@Component
@Slf4j
public class ReadArticleSubscriber implements ApplicationListener<ReadArticleEvent> {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private StatisticsArticlePVMapper statisticsArticlePVMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(ReadArticleEvent event) {
        // 处理收到事件
        Long articleId = event.getArticleId();

        // 获取当前线程 名称
        String threadName = Thread.currentThread().getName();

        log.info("==> 当前线程为：{}", threadName);
        log.info("==> 文章阅读事件消费成功, articleId: {}", articleId);
        articleMapper.increaseReadNum(articleId);
        log.info("==> 文章阅读量 +1 操作成功，articleId: {}", articleId);

        // 当日文章 PV 访问量 +1
        LocalDate currDate = LocalDate.now();
        statisticsArticlePVMapper.increasePVCount(currDate);
        log.info("==> 当日文章 PV 访问量 +1 操作成功，date: {}", currDate);
    }
}
